<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
								xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
								xmlns:s="http://www.w3.org/2001/XMLSchema"
								xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
    <!-- WSDL transformation document -->
    <xsl:include href="file:///C:/Users/csbeks/Documents/Visual Studio 2008/Projects/Novice/Novice1/Services/Transformation/WSDL/wsdl-t.xsl"/>
    <xsl:output method="html" encoding="utf-8"/>
    <!-- Task Model -->
    <xsl:variable name="TM-OLH" select="document('file:///C:/Users/csbeks/Documents/Visual Studio 2008/Projects/Novice/Novice1/Knowledge Base/Task Model/TaskModel.xml')"/>
    <!-- Variable with all elements that hold a @wsdl attribute-->
    <xsl:variable name="reliants" select="$TM-OLH//*[@reliant]"/>
    <!-- Applied Style Sheet -->
    <xsl:variable name="css">
        /**
        =========================================
        RESET
        =========================================
        */
        body {
        margin: 0;
        padding: 0;
        height: auto;
        color: black;
        background-color: #ffffff;
        font: normal 80%/120% Arial, Helvetica, sans-serif;
        }
        /**
        =========================================
        CONTENT
        =========================================
        */
        h4,h3,h2,h1{ 			/* matches "/container" */
        margin:5px;
        }
        h3,h4{
        margin-bottom:10px;
        }
        /**************************** START Header Stylings ************************************/
        .h2{
        width:100%;
        color:black;
        background-color: #ccc;
        border-bottom:1px #000 solid;
        padding: 15px;
        }
        /**************************** END Header Stylings ************************************/
        .l-container{ /* container holding only leaf nodes */
        margin:25px;
        padding:15px;
        /*	border:1px solid #000;
        background-color: gray;		*/
        }
        .container{
        margin:25px;
        background-color: #ccc;
        }
        .border{
        margin:25px;
        border:1px solid #000;
        }
        h3.container{
        margin:0px;
        }
        .header{
        margin:5px;
        }
        .leaf{
        margin:15px;
        border:1px solid #000;
        /*	background-color: gray;		*/
        }
        =========================================
        LAYOUT
        =========================================
        .OH{	/* ORDERED HORIZONTAL GROUP */
        float:left;
        display:inline;
        /*width:auto;*/
        }
        .OV{	/* ORDERED VERTICAL GROUP */
        display:block;
        width:auto;
        }
        .PH{	/* PLAIN VERTICAL GROUP */
        }
        .PV{	/* PLAIN HORIZONTAL GROUP */
        }
        .P{		/* PLAIN GROUP */
        }
        .PR{
        float:right;
        }
        .V{		/* ORDERED GROUP */
        }
        .left{
        float:left;
        }
        =========================================
        CONTROLS
        =========================================
        .button{
        background-color:blue;
        color:white;
        border:1px solid #000;
        padding:5px;
        }
        .buttonholder{
        width:100%;
        }
        =========================================
        Generated HTML
        =========================================
        .operation{
        margin:0 0 10px 0;
        padding:0 0 5px 5px;
        }

        div.display{

        color:#000000;
        border:solid 1px #b3b3b3;
        background:#dcdcdc;

        width:170px;
        height: 25px;
        margin:5px 5px 0px 5px;

        float:right;
        display:inline;
        position:relative;

        padding: 2px;
        }

        .input{
        margin:5px 5px 4px 5px;
        position:relative;
        }

        .inputName{
        width:125px;
        display:inline;
        float:left;
        font-weight:normal;
        height: 25px;
        margin:5px 5px 0px 5px;
        position:relative;
        }

        input, select{
        width:170px;
        height: 25px;
        font-size:14px;
        }

        input.radio{
        margin:0px 5px 20px 5px;
        padding:0;
        width:15px;
        }
        textArea{
        margin:0px 5px 20px 5px;
        padding:0;
        width:300px;
        height:100px;
        overflow: scroll;
        overflow-y: scroll;
        overflow-x: hidden;
        overflow:-moz-scrollbars-vertical;
        }
        .listBox{
        padding:0;
        width:300px;
        height:100px;
        }
        .submitButton{
        color:#ffffff;
        background-color:#003366;
        width:100px;
        float:left;
        margin-right:40px;
        margin-bottom:20px;
        border:1px #000000 solid;
        padding:5px;
        text-align:center;
        cursor:pointer;
        }
        .submitButton:hover{
        color:#003366;
        background-color:#ffffff;
        width:100px;
        float:left;
        margin-right:40px;
        margin-bottom:20px;
        border:1px #000000 solid;
        padding:5px;
        text-align:center;
        }
    </xsl:variable>
    <!-- Root -->
    <xsl:template match="/">
        <xsl:variable name="layout" select="@layout"/>
        <style type="text/css">
            <xsl:value-of select="$css" disable-output-escaping="yes"/>
        </style>
        <xsl:apply-templates select="container"/>
        <!--javascript for reliant items: for each element with the attribute 'reliant', generate jscript that changes its value based on the value in the element it relies on-->
        <script type="text/javascript">
          <![CDATA[  
          $(function(){
            //expireLiveQueryEvents();
          });
           ]]>
          <xsl:for-each select="$reliants">
                <!--the reliant attribute "name" for the current node-->
                <xsl:variable name="var.reliant" select="current()/@reliant" />
                <!-- the actual node taken from the task model-->
                <xsl:variable name="cur.reliant" select="$TM-OLH//*[@operation = $var.reliant]" />
                <xsl:if test="current()/@reliant">
                    <xsl:choose>
                        <xsl:when test="current()/@reliant2">
                            <![CDATA[    
                                //Two element Reliance                                
                                $("#]]><xsl:value-of select="current()/@reliant"/><![CDATA[").livequery('keypress', function(e){
                                    //invokes the dependent method with the 2 values from the independents
                                    //parameters: parent1, parent2, child, wsdl
                                    if (e.which == 13) {
                                      fillDependents(']]><xsl:value-of select="current()/@reliant"/><![CDATA[',']]><xsl:value-of select="current()/@reliant2"/><![CDATA[',']]><xsl:value-of select="current()/@operation"/><![CDATA[',']]><xsl:value-of select="current()/@wsdl"/><![CDATA[', false);
                                      e.stopPropagation();
                                      return false;      
                                    }                  
                                });  
                            ]]>
                        </xsl:when>
                        <xsl:otherwise>
                            <![CDATA[                    
                                $("#]]><xsl:value-of select="current()/@reliant"/><![CDATA[").livequery('change', function(e){
                                    //invokes the dependent method with the value from the independent
                                    //parameters: parent, child, wsdl
                                    fillDependent(']]><xsl:value-of select="current()/@reliant"/><![CDATA[',']]><xsl:value-of select="current()/@operation"/><![CDATA[',']]><xsl:value-of select="current()/@wsdl"/><![CDATA[', false);
                                    e.stopPropagation();
                                    return false; 
                                });
                                $("#]]><xsl:value-of select="current()/@reliant"/><![CDATA[").livequery('keypress', function(e){
                                    //invokes the dependent method with the value from the independent
                                    //parameters: parent, child, wsdl
                                     if (e.which == 13) {
                                        fillDependent(']]><xsl:value-of select="current()/@reliant"/><![CDATA[',']]><xsl:value-of select="current()/@operation"/><![CDATA[',']]><xsl:value-of select="current()/@wsdl"/><![CDATA[', false);
                                        e.stopPropagation();
                                        return false;
                                     } 
                                });
                                ]]>                            
                            <!-- If the task model item exists then add it-->
                            <xsl:if test="current()/@TM">
                                <![CDATA[  
                                //Task Model Reliance
                                    $(function(){
                                        if("]]><xsl:value-of select="current()/@TM"/><![CDATA[" !== ""){                              // If a reliant exists in the task model, then begin the 'load from task model' process.
                                            if($("#]]><xsl:value-of select="current()/@reliant"/><![CDATA[").is(':visible') == false){      // If reliant (what we rely on) is NOT visible..then load data from reliant two in task model/working data file
                                              fillDependent(getReliant2(']]><xsl:value-of select="current()/@TM"/><![CDATA['),']]><xsl:value-of select="current()/@operation"/><![CDATA[',']]><xsl:value-of select="current()/@wsdl"/><![CDATA[', true);
                                              e.stopPropagation();
                                              return false;                        
                                            }                            
                                        }
                                    });               
                                ]]>
                            </xsl:if>                            
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:if>
            </xsl:for-each>
          <![CDATA[
          //function detaches all livequery events from our reliant objects: This should stop reliant items from firing continuously when step restarts
          function expireLiveQueryEvents(){ ]]>  
            <xsl:for-each select="$reliants">
              <!--the reliant attribute "name" for the current node-->
              <xsl:variable name="var.reliant" select="current()/@reliant" />
              <![CDATA[                    
                  $("#]]><xsl:value-of select="$var.reliant"/><![CDATA[").expire('change');
                  $("#]]><xsl:value-of select="$var.reliant"/><![CDATA[").expire('keypress');
                  ]]>
            </xsl:for-each>
         <![CDATA[ 
         }
         ]]>
        </script>
    </xsl:template>
    <!-- template for container with child container -->
    <xsl:template match="container[container]">
        <xsl:variable name="layout" select="@layout"/>
        <xsl:variable name="border" select="@border"/>
        <div class="{$border} {$layout} left">
            <xsl:choose>
                <xsl:when test="current()[@onComplete]">
                    <div class="h2">
                        <h2>
                            <xsl:value-of select="@name"/>
                        </h2>
                    </div>
                </xsl:when>
                <xsl:otherwise>
                    <h4>
                        <xsl:value-of select="@name"/>
                    </h4>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates select="container"/>
            <!-- If current node is root, then add the next step button here -->
            <xsl:if test="current()[@onComplete]">
                <script type="text/javascript">
                    <![CDATA[setStep(']]><xsl:value-of select="@step" /><![CDATA[');]]>
                </script>
                <div class="buttonholder">
                    <div class="submitButton" onClick="nextStep({@step});">
                        <xsl:value-of select="@onComplete"/>
                    </div>
                </div>
            </xsl:if>
        </div>
    </xsl:template>
    <!-- template for leaf container -->
    <xsl:template match="container">
        <xsl:variable name="layout" select="@layout"/>
        <xsl:variable name="cont.name" select="@name"/>
        <div class="l-container {$layout}">
            <xsl:choose>
                <!-- Add loader image to dependent-->
                <xsl:when test="$TM-OLH//*[@name = $cont.name]/@reliant">
                    <div class="depholderdiv">
                        <img class="deploader" src="css/deploader.gif" alt="loading..."/>
                    </div>
                </xsl:when>
            </xsl:choose>

            <xsl:if test="$TM-OLH//*[@name = $cont.name]">
                <!-- get wsdl url -->
                <xsl:variable name="cont.wsdl.loc" select="$TM-OLH//*[@name = $cont.name]/@wsdl"/>
                <!-- load wsdl (xml) file into variable -->
                <xsl:variable name="cont.wsdl" select="document($cont.wsdl.loc)"/>
                <!-- Apply transformation to relevant wsdl elements-->
                <xsl:apply-templates select="$cont.wsdl/wsdl:definitions/wsdl:portType"/>
                <!-- 
					NOTE: Find a way of only storing the service name instead of the entire url.
					e.g. "SearchCustomer" instead of "http://localhost:12000/Services/SearchCustomer.asmx?WSDL".
					Files: TaskModel.xml and OLH.xsl
				-->
            </xsl:if>
        </div>
    </xsl:template>
    <!-- 		HOW IT WORKS:
		For every container that has a wsdl attribute, 
		1.	Check for element with the same name in the task model. 
		2.	If names are equal, check for wsdl. if exists, get the wsdl location from the attribute
		3.	import it as xml
		4.	transform it using the transformation xsl: $WSDL-T
	-->
</xsl:stylesheet>